package com.sugree.twitter.views;

import java.io.IOException;
import java.util.Vector;
import java.util.Enumeration;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import com.substanceofcode.tasks.AbstractTask;
import com.substanceofcode.utils.Log;
import com.sugree.twitter.TwitterController;

public class WaitScreen extends Form implements CommandListener,Runnable {
	private TwitterController controller;
	private AbstractTask task;

	private StringItem waitField;
	private Command cancelCommand;

	private String waitText = "Please wait...";
	private Thread thread;

	public WaitScreen(TwitterController controller, AbstractTask task) {
		super("Wait");
		this.controller = controller;
		this.task = task;

		thread = new Thread(this);
		thread.start();

		waitField = new StringItem("Please wait...", waitText);
		append(waitField);

		cancelCommand = new Command("Cancel", Command.STOP, 1);
		addCommand(cancelCommand);

		setCommandListener(this);
	}

	public void setWaitText(String text) {
		waitText = text;
		waitField.setText(text);
	}

	public void commandAction(Command cmd, Displayable display) {
		if (cmd == cancelCommand) {
			controller.showTimeline();
		}
	}

	public void run() {
		task.execute();
	}
}
